package com.jf.apply_car.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jf.apply_car.bean.Apply;
import com.jf.apply_car.bean.Userinfo;
import com.jf.apply_car.mapper.ApplyMapper;
import com.jf.apply_car.service.ApplyService;
import com.jf.apply_car.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApplyServiceImpl implements ApplyService {
    @Autowired
    private ApplyMapper applyMapper;

    public void apply(Apply apply) {
        /*获取当前登录用户*/
        /*封装申请人编号*/
        apply.setApplyUserinfoId(LoginUtil.getLoginInfoId());
        /*根据申请人的角色设置申请状态*/
        if (LoginUtil.getLoginRoleId() == MyConstants.BOSS_ROLE_ID) {
            apply.setApplyStatus(ApplyStatus.BU_MEN_TONG_GUO);
            /*当前用户为负责人时，订单中的部门审核人就是当前用户*/
            apply.setExamDepartmentUserinfoId(LoginUtil.getLoginInfoId());
            apply.setExamDepartmentTime(new Date());
            apply.setExamDepartmentContent("同意");
        } else if (LoginUtil.getLoginRoleId() == MyConstants.EMPLOYEE_ROLE_ID) {
            apply.setApplyStatus(ApplyStatus.YI_SHEN_QING);
        } else {
            throw new BusinessException("禁止司机或管理员申请用车");
        }
        apply.setApplyTime(new Date());
        applyMapper.insertSelective(apply);
    }

    public ResultJson getMineApplyPage(int page, int limit, Apply apply) {

        apply.setApplyUserinfoId(LoginUtil.getLoginInfoId());

        PageHelper.startPage(page, limit);
        List<Apply> list = applyMapper.getMineApply(apply);
        PageInfo<Apply> pageInfo = new PageInfo<Apply>(list);
        return ResultJson.page(pageInfo.getTotal(), pageInfo.getList());
    }

    public void delete(int[] ids) {
        applyMapper.deleteBatch(ids);
    }

    public ResultJson getAllPage(int page, int limit, Apply apply) {
        Userinfo loginInfo = LoginUtil.getLoginInfo();
        if (loginInfo.getRoleId() == 2) {
            apply.setDepartmentId(loginInfo.getDepartmentId());
        }
        if (loginInfo.getRoleId() == 3) {
            apply.setApplyUserinfoId(loginInfo.getUserinfoId());
        }
        if (loginInfo.getRoleId() == 4) {
            apply.setDriverId(loginInfo.getUserinfoId());
        }

        PageHelper.startPage(page, limit);
        List<Apply> list = applyMapper.getAll(apply);
        PageInfo<Apply> pageInfo = new PageInfo<Apply>(list);
        return ResultJson.page(pageInfo.getTotal(), pageInfo.getList());
    }
}
